<?php
/**
 * Created by xiaokengge
 * User: zhaoyu
 * Date: 2024/1/16
 * Time: 18:22
 *describe:****
 */

namespace app\api\service;


use think\exception\HttpResponseException;
use think\Request;
use think\Response;

class ThrowNewErr
{

    public function validateFailException($msg, $data = null, $code = 0, $type = null, array $header = [])
    {
        $result = [
            'code' => $code,
            'msg'  => $msg,
            'time' => Request::instance()->server('REQUEST_TIME'),
            'data' => $data,
        ];
        // 如果未设置类型则自动判断
        $type = $type ? $type : (request()->param(config('var_jsonp_handler')) ? 'jsonp' :'json');

        if (isset($header['statuscode'])) {
            $code = $header['statuscode'];
            unset($header['statuscode']);
        } else {
            //未设置状态码,根据code值判断
            $code = $code >= 1000 || $code < 200 ? 200 : $code;
        }
        $response = Response::create($result, $type, $code)->header($header);
        throw new HttpResponseException($response);
    }






}
